<?php
namespace Admin\Model;
use Think\Model;
class ArticleModel extends Model 
{

	protected $insertFields = array('title','author','click_count','is_hot','keywords','description','content','site_id','cat_id');
	protected $updateFields = array('id','title','author','click_count','is_hot','keywords','description','content','site_id','cat_id');
	protected $_validate = array(
		array('title', 'require', '文章标题不能为空！', 1, 'regex', 3),
		array('title', '1,45', '文章标题的值最长不能超过 45 个字符！', 1, 'length', 3),
		// array('Class', 'require', '主分类的id不能为空！', 1, 'regex', 3),
		// array('Class', 'number', '主分类的id必须是一个整数！', 1, 'regex', 3),
	);

	public function search($pageSize = 10, $isDelete = 1)
	{
		// $where['a.state']= array('eq', !empty($isDelete)?1:0); 	//是否删除	
		if($title = I('get.title'))
			$where['a.title'] = array('like', "%$title%");
		if($Class = I('get.Class')){
			$where['b.id|e.id'] = array('eq', $Class);
		}
		if($admin_id = I('get.admin_id'))
			$where['a.admin_id'] = array('eq', $admin_id);
		//排序
		$order = 'a.id desc';
		if(I('get.sort_id')){		//防止跟id冲突
			$sort = I('get.sort_id');
			$order = 'a.id '.$sort;
		}
		if(I('get.add_time')){
			$sort = I('get.add_time');	
			$order = 'a.add_time '.$sort;
		}
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize,$_GET);
		// 配置翻页的样式
        $page->setConfig('prev', '&lt;&lt;');
        $page->setConfig('next', '&gt;&gt;');
        $data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		//获取文章关联的分类
		$db_prefix = C('DB_PREFIX');
		$join = 'LEFT JOIN '.$db_prefix.'site b ON b.id=a.site_id';
		$data['data'] = $this->field('a.*,b.site_name')->alias('a')->join($join)->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		//当前排序
		$data['count'] = $count;
		
		return $data;
	}

	public function paper($pageSize = 10){
		if($title = I('get.title'))
			$where['title'] = array('like', "%$title%");
		$count = M('xwcb')->where($where)->count();
		$page = new \Tool\Page($count,$pageSize);
		// 配置翻页的样式
		$data['page'] = $page->fpage(array(0,1,2,3,4,5,6,7,8));
		/************************************** 取数据 ******************************************/
		//获取文章关联的分类
		$db_prefix = C('DB_PREFIX');
		$data['data'] = M('xwcb')->field('*')->where($where)->order('id desc')->limit($page->limit)->select();
		
		return $data;
	}

	// 添加前
	protected function _before_insert(&$data, $option)
	{
		$type = I('get.type','1','intval');
		$data['add_time'] = time();
		//处理图片
		if(isset($_FILES['Thumbnails']) && $_FILES['Thumbnails']['error'] == 0)
		{
			$ret = uploadOne('Thumbnails','article'); //接收
			if($ret['ok'] == 1)
			{
				$data['Thumbnails'] = '/uploadfile/'.$ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}

	protected function _after_insert($data, $option)
	{
		//获取文章的id
		// $doctor_ids = I('post.doctor_ids');
		// if($doctor_ids){
		// 	$diModel = M('ArticleDoctor');
		// 	foreach($doctor_ids as $k=>$v){
		// 		if(empty($v)){
		// 			continue;
		// 		}else{
		// 			$diModel->add(
		// 				array(
		// 					'article_id'=>$data['id'],
		// 					'doctor_id'=>$v,
		// 				)
		// 			);
		// 		}
		// 	}
		// }
	}

	// 修改前
	protected function _before_update(&$data, $option)
	{
		//处理图片
		if(isset($_FILES['Thumbnails']) && $_FILES['Thumbnails']['error'] == 0)
		{
			$ret = uploadOne('Thumbnails','article'); //接收
			if($ret['ok'] == 1)
			{
				$data['Thumbnails'] = '/uploadfile/'.$ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_Thumbnails'),  //删除
			));
		}
	}

	protected function _after_update($data, $option)
	{
		$adModel = M('ArticleDoctor');

		$adModel->where(array('article_id'=>array('eq', $option['where']['id'])))->delete();
		if($doctor_ids){
			foreach($doctor_ids as $k=>$v){
				if(empty($v)){
					continue;
				}else{
					$adModel->add(
						array(
							'article_id'=>$data['id'],
							'doctor_id'=>$v,
						)
					);
				}
			}
		}
	}

	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('logo')->find($option['where']['id']);
		deleteImage($images);
		//删除其他信息
		$model = M('DoctorIllness');
		$model->where(array('article_id'=>array('eq', $option['where']['id'])))->delete();

	}


	/************************************ 其他方法 ********************************************/

	//获取录入者
	public function getAdmins(){
		$db_prefix = C('DB_PREFIX');
		$res = M('Admin')->field('id,name')->select();
		return $res;
	}

	//获取文章
	public function getIllness()
	{
        $db_prefix = C('DB_PREFIX');
		$sql ="select id,title from ".$db_prefix."Disease where type=0 and class=1 ORDER BY orders Asc,Id Desc";
		$ret = $this->query($sql);
		foreach ($ret as $key => $value) {
			$sql = "select id,title from ".$db_prefix."Disease where type='".$value['id']."' and class=1 ORDER BY orders Asc,Id Desc";
			$ret[$key]['info'] = $this->query($sql);
		}
		// $data = array();
		// foreach($ret as $k=>$v){
		// 	$data[$v['cat_name']][] = $v;
		// }
		return $ret;
	}


	public function getArticleByType($type,$nums=5){
		$ac = D('Admin/ArticleType');
        //$childs = $ac->getChildren(1);
		//$childs = implode(',',$childs);
		//$articles = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'article_type b on a.cat_id=b.id')->where(array('cat_id'=>array('in',$childs)))->limit(5)->select();
		$articles = $this->field('a.*')->alias('a')->where(array('cat_id'=>array('eq',$type)))->order('sort_num desc,id asc')->limit($nums)->select();
		return  $articles;
	}


	public function getArticleByType2($type,$nums){
		if(!empty($nums)){
			$articles = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'article_type b on a.cat_id=b.id')->where(array('cat_id'=>array('eq',$type)))->order('a.sort_num desc')->limit($nums)->select();
		}
		$articles = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'article_type b on a.cat_id=b.id')->where(array('cat_id'=>array('eq',$type)))->order('a.sort_num desc')->select();
		return  $articles;

	}


	public function getIllnessAticles($type,$illness_id,$nums=3){

		if(!empty($nums)){
			$articles = $this->field('a.article_name,a.id,a.logo,a.addtime,a.article_desc,a.author')->alias('a')->join('left join '.C('DB_PREFIX').'articles_category b on a.cat_id=b.id
			left join '.C('DB_PREFIX').'illness c on c.id=a.illness_id')
					->where(array('a.cat_id'=>array('eq',$type),'a.illness_id'=>array('eq',$illness_id)))->order('a.sort_num desc')->limit($nums)->select();
		}
		return $articles;

	}


	public function getSelectedArticle($nums=3){
		//9  20
		$ac = D('Admin/ArticleType');
		$childSpecial = $ac->getChildren(9);
		$childWomen = $ac->getChildren(20);
		$all = array_merge($childSpecial, $childWomen);
		$childs = implode(',',$all);
		$articles = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'article_type b on a.cat_id=b.id')->where(array('cat_id'=>array('in',$childs),'is_hot'=>array('eq','1')))->order('sort_num desc,id asc')->limit($nums)->select();
		return $articles;

	}


	public function getArticleById($id){
		$res = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'articles_category b on a.cat_id=b.id')->where(array('a.id'=>array('eq',$id)))->find();

		return $res;
	}

	public function getNext($id,$cat_id){
		$res = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'articles_category b on a.cat_id=b.id')->where(array('a.cat_id'=>array('eq',$cat_id),'a.id'=>array('gt',$id)))->order('a.id asc ')->limit(1)->find();
		return $res;
	}

	public function getLast($id,$cat_id){
		$res = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'articles_category b on a.cat_id=b.id')->where(array('a.cat_id'=>array('eq',$cat_id),'a.id'=>array('lt',$id)))->order('a.id desc ')->limit(1)->find();
		return $res;

	}

	public function getRelated($id,$cat_id,$nums=3){
		$res = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'articles_category b on a.cat_id=b.id')->where(array('a.cat_id'=>array('eq',$cat_id),'a.id'=>array('neq',$id)))->order('a.sort_num desc,a.id asc')->limit($nums)->select();
		return $res;
	}


	//获取分类的文章列表
	public function myList($pageSize = 5,$type)
	{

		if($type = I('get.type'))
			$where['cat_id'] = array('eq', $type);

		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		//获取文章关联的分类
		$db_prefix = C('DB_PREFIX');
//		$data['data'] = $this->field('a.*,b.cat_name,c.ill_name,GROUP_CONCAT(e.doctor_name) as doctor_names')->alias('a')->
//		join('LEFT JOIN '.$db_prefix.'article_type b ON a.cat_id=b.id left join '.$db_prefix.'illness c on c.id=a.illness_id
//		       left join '.$db_prefix.'article_doctor d on d.article_id=a.id left join '.$db_prefix.'doctors e on d.doctor_id=e.id')->
//		where($where)->group('a.id')->order('id DESC')->limit($page->firstRow.','.$page->listRows)->select();

		$data['data'] = $this->field('a.*,b.cat_name')->alias('a')->
		join('LEFT JOIN '.$db_prefix.'article_type b ON a.cat_id=b.id')->
		where($where)->order('sort_num desc,id asc')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}

	public function getTypeById($id){
		$model = M('article_type');
		$res = $model->where(array('id'=>array('eq',$id)))->find();
		return $res;

	}

	public function getCases($type,$nums){
		$res = $this->where(array('cat_id'=>array('eq',$type)))->limit($nums)->select();
		return $res;
	}


	public function getAllArticle($pageSize){
		$ac = D('Admin/ArticleType');
		$childSpecial = $ac->getChildren(9);
		$childWomen = $ac->getChildren(20);
		$all = array_merge($childSpecial, $childWomen);
		$childs = implode(',',$all);


		$count = $this->alias('a')->join('left join '.C('DB_PREFIX').'article_type b on a.cat_id=b.id')->where(array('cat_id'=>array('in',$childs)))->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		$data['data'] = $this->field('a.*,b.cat_name')->alias('a')->join('left join '.C('DB_PREFIX').'article_type b on a.cat_id=b.id')->where(array('cat_id'=>array('in',$childs)))->order('sort_num desc,id asc')->limit($page->firstRow.','.$page->listRows)->select();
			return $data;

	}


	public function getDoctorBedArticle($id){
		$res = $this->where(array('doctor_id'=>array('eq',$id)))->limit(4)->select();
		return $res;
	}


}
























